# 腾讯元宝图片生成
import urllib
from django.http import JsonResponse
from web.ais.TengXunYuanBao import GenerateImages, ImageEdit
from django.http import StreamingHttpResponse

generate_images = GenerateImages.TengXunYuanBao_GenerateImages()
image_edit = ImageEdit.TengXunYuanBao_ImageEdit()

def get_style(request):
    return JsonResponse(generate_images.get_style(), safe=False)

# 图片生成
def generate_image(request):
    prompt = request.GET.get('prompt')
    style = request.GET.get('style')
    chat_id = request.GET.get('chat_id')
    response = generate_images.generate_images(prompt, style, chat_id)
    response = StreamingHttpResponse(generate_images.handle_stream_response(response), content_type='text/event-stream')
    response.headers['Cache-Control'] = 'no-cache'
    return response
    

# 图片去除水印
def remove_watermark(request):
    img_path = request.GET.get('img_path')
    # 进行url解码
    img_path = urllib.parse.unquote(img_path) 
    print(img_path)   
    response = image_edit.remove_watermark(img_path)
    response = StreamingHttpResponse(image_edit.handle_stream_response(response), content_type='text/event-stream')
    response.headers['Cache-Control'] = 'no-cache'
    return response
    

# 图片变清晰
def sharpen(request):
    img_path = request.GET.get('img_path')
    img_path = urllib.parse.unquote(img_path) 
    response = image_edit.sharpen(img_path)
    response = StreamingHttpResponse(image_edit.handle_stream_response(response), content_type='text/event-stream')
    response.headers['Cache-Control'] = 'no-cache'
    return response
